package com.haixee.parkdemo.auth.service.impl;

import com.haixee.parkdemo.auth.dao.JobMapper;
import com.haixee.parkdemo.auth.dao.OrganizeCustomMapper;
import com.haixee.parkdemo.auth.dao.OrganizeMapper;
import com.haixee.parkdemo.auth.dao.UserCustomMapper;
import com.haixee.parkdemo.auth.enums.JobType;
import com.haixee.parkdemo.auth.enums.OrganizeType;
import com.haixee.parkdemo.auth.model.Job;
import com.haixee.parkdemo.auth.model.Organize;
import com.haixee.parkdemo.auth.service.OrganizeService;
import com.haixee.parkdemo.common.context.WebContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class OrganizeServiceImpl implements OrganizeService {

    @Autowired
    private OrganizeMapper organizeMapper;

    @Autowired
    private OrganizeCustomMapper organizeCustomMapper;

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private UserCustomMapper userCustomMapper;

    @Override
    @Transactional
    public void addOrganize(Integer tenant, Integer parentId, Byte type, String name, Boolean isManage) {

        Job job = new Job();
        job.setName(name);
        job.setJobType(JobType.ORGANIZE.getCode());
        jobMapper.insertSelective(job);

        Organize organize = new Organize();
        organize.setId(job.getId());
        organize.setName(name);
        organize.setParentId(parentId);
        organize.setId(job.getId());
        organize.setType(type);

        if (tenant == null) {
            organize.setTenantId(WebContextHolder.getAuth().getTenantId());
        } else {
            organize.setTenantId(tenant);
        }

        if (type == OrganizeType.JOB.getCode()) {
            organize.setIsManage(isManage);
        }

        organizeCustomMapper.insertSelective(organize);
    }

    @Override
    public void updateOrganize(Integer id, String name) {

        Organize organize = new Organize();
        organize.setId(id);
        organize.setName(name);

        organizeMapper.updateByPrimaryKeySelective(organize);
    }

    @Override
    public List<Organize> getAll(Integer tenantId) {
        return organizeCustomMapper.selectAll(tenantId);
    }

    @Override
    public List<Integer> getUnderTree(Integer uid) {

//        User user = userMapper.selectByPrimaryKey(uid);

        Set<Integer> jobIdListS = userCustomMapper.selectJobIdByUid(uid);
        List<Integer> jobIdList = new ArrayList<>(jobIdListS);

        List<Integer> allJobIdList = new ArrayList<>();

        for (int i = 0; i < jobIdList.size(); i++) {
            List<Integer> children = organizeCustomMapper.getChildren(jobIdList.get(i));
            for (int i1 = 0; i1 < children.size(); i1++) {
                for (Integer child : organizeCustomMapper.getChildren(children.get(i1))) {
                    if (!children.contains(child)) {
                        children.add(child);
                    }
                }
            }

            allJobIdList.addAll(children);
        }

        return allJobIdList;
    }

    @Override
    public List<Integer> getUnderTreeById(Integer id) {

        List<Integer> children = organizeCustomMapper.getChildren(id);
        for (int i1 = 0; i1 < children.size(); i1++) {
            children.addAll(organizeCustomMapper.getChildren(children.get(i1)));
        }
        return children;
    }

}
